<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * The Request Dispatcher
 * 
 * A dispatcher dispatches requests to an apropriate handler, invokes a filter chain
 * and covers view resolution. It is responsible to coordinate interaction between
 * model, view and controllers. Moreover, dispatch exceptions could be caught and 
 * are passed to a request handler exception resolver to e.g. forward to an error 
 * page.
 *	
 * @package auxo
 * @subpackage Web	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_RequestDispatcher {
	
	protected $handlerResolver = NULL;
	
	protected $viewResolver = NULL;
	
	protected $exceptionResolver = NULL;

	/**
	 * Creates a dispatcher
	 *
	 * @param tx_auxo_RequestHandlerResolverInterface $handlerResolver
	 * @param tx_auxo_ViewResolverInterface $viewResolver
	 * @param tx_auxo_RequestExceptionResolverInterface $exceptionResolver
	 * @param tx_auxo_FilterChain $filterChain
	 */
	public function __construct(tx_auxo_RequestHandlerResolverInterface $handlerResolver,
	                            tx_auxo_ViewResolverInterface $viewResolver,
	                            tx_auxo_RequestExceptionResolverInterface $exceptionResolver) {
		$this->handlerResolver = $handlerResolver;
		$this->viewResolver = $viewResolver;
		$this->exceptionResolver = $exceptionResolver;
	}
	
	/**
	 * Dispatch requests to an apropriate handler and also takes care of view and
	 * exception resolution.
	 *
	 * @return void
	 */
	public function dispatch() {
		if (! ($requestHandler = $this->handlerResolver->resolve())) {
			throw new tx_auxo_DispatchException('Request Handler could not be resolved');
		}
		
		try {
			$modelView = $requestHandler->handle($request, $response);
		}
		catch (Exception $exception) {
			if (! $this->exceptionResolver) {
				throw $exception;
			}
			$this->exceptionResolver->handle($request, $response, $exception);
		}
		
		if ($modelView) {
			if ( !($view = $this->viewResolver->resolve($modelView))) {
				throw new tx_auxo_DispatchException('View could not be resolved');
			}
			
			$view->render($modelView);
		}
	}
}
?>